package cn.ulc.socket.admin.service.staff.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.util.CalendarUtil;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.staff.BillDetailDTO;
import cn.ulc.socket.admin.model.dto.staff.BillListDTO;
import cn.ulc.socket.admin.service.staff.StaffDeductBillServ;
import cn.ulc.store.dao.staff.StaffDeductBillDao;
import cn.ulc.store.entity.Company;
import cn.ulc.store.entity.staff.Staff;
import cn.ulc.store.entity.staff.StaffDeductBill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 骑手提成账单
 */
@Service
public class StaffDeductBillServImpl extends BaseService implements StaffDeductBillServ {
    private static final Log log = LogFactory.get();

    @Autowired
    StaffDeductBillDao billDao;

    @Override
    public BillListDTO findRiderBillList(Long riderId,String month, Integer year, String type) {
        Date date = new Date();
        List<StaffDeductBill> billList = new ArrayList<>();
        List<Map<String,Object>> dayList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        if("month".equals(type)){
            billList = billDao.findByDate(riderId,month);
            // 获得当前日期对象
            cal.clear();// 清除信息
            cal.set(Calendar.YEAR, Integer.parseInt(month.split("-")[0]));
            cal.set(Calendar.MONTH, Integer.parseInt(month.split("-")[1])-1);// 1月从0开始
            // 当月1号
            cal.set(Calendar.DAY_OF_MONTH,1);
            int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            for (int j = 1; j <= count ; j++) {
                Date calDate = cal.getTime();
                if(date.compareTo(calDate) >0) {
                    Map<String,Object> map = new HashMap<>();
                    map.put("date",dateFormat.format(calDate));
                    map.put("incomeAmount", BigDecimal.ZERO);
                    map.put("incomeCount",0);
                    map.put("expendAmount",BigDecimal.ZERO);
                    map.put("expendCount",0);
                    dayList.add(map);
                    cal.add(Calendar.DAY_OF_MONTH, 1);
                }
            }
        }
        if("year".equals(type)){
            billList = billDao.findByDate(riderId,year+"");
            dateFormat = new SimpleDateFormat("yyyy-MM");
            cal.clear();// 清除信息
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, 0);// 1月从0开始
            cal.set(Calendar.DAY_OF_MONTH,1);
            for(int i = 0; i < 12; i ++){
                Date calDate = cal.getTime();
                if(date.compareTo(calDate) >0) {
                    Map<String,Object> map = new HashMap<>();
                    map.put("date",dateFormat.format(calDate));
                    map.put("incomeAmount",BigDecimal.ZERO);
                    map.put("incomeCount",0);
                    map.put("expendAmount",BigDecimal.ZERO);
                    map.put("expendCount",0);
                    dayList.add(map);
                    cal.add(Calendar.MONTH, 1);
                }
            }
        }
        for(StaffDeductBill bill:billList){
            String createAt = CalendarUtil.dateToString(bill.getCreateAt(),dateFormat);
            for(Map<String,Object> day:dayList){
                if(createAt.equals(day.get("date"))){
                    if("+".equals(bill.getEiType())){
                        BigDecimal incomeAmount = (BigDecimal)day.get("incomeAmount");
                        day.put("incomeAmount",incomeAmount.add(bill.getAmount()));
                        Integer incomeCount = (Integer)day.get("incomeCount");
                        day.put("incomeCount",(incomeCount+1));
                    }
                    else if("-".equals(bill.getEiType())){
                        BigDecimal expendAmount = (BigDecimal)day.get("expendAmount");
                        day.put("expendAmount",expendAmount.add(bill.getAmount()));
                        Integer expendCount = (Integer)day.get("expendCount");
                        day.put("expendCount",(expendCount+1));
                    }
                }
            }
        }
        return new BillListDTO(dayList);
    }

    @Override
    public BillDetailDTO findRiderBillDetail(String beginDate, String endDate) {
        BillDetailDTO dto =new BillDetailDTO();
        Staff loginUser = getCurrentUser();
        List<Map<String,Object>> list = billDao.findByType(beginDate,endDate,loginUser.getId());
        StaffDeductBill bill = billDao.findByDealDate(beginDate,endDate,loginUser.getId());
        BigDecimal balance = BigDecimal.ZERO;
        if(bill!=null) balance = bill.getBalance();
        List<Map<String,Object>> incomeList = new ArrayList<>();
        List<Map<String,Object>> expendList = new ArrayList<>();
        BigDecimal incomeAmount = BigDecimal.ZERO;
        BigDecimal expendAmount = BigDecimal.ZERO;
        if(list.size() > 0){
            for(Map<String,Object> map:list){
                String category = (String)map.get("category");
                BigDecimal amount = (BigDecimal)map.get("amount");
                if("+".equals(category)){
                    incomeList.add(map);
                    incomeAmount = incomeAmount.add(amount);
                }
                if("-".equals(category)){
                    expendList.add(map);
                    expendAmount = expendAmount.add(amount);
                }
            }
        }
        dto.setBalance(balance);
        dto.setIncomeList(incomeList);
        dto.setIncomeAmount(incomeAmount);
        dto.setExpendList(expendList);
        dto.setExpendAmount(expendAmount);
        return dto;
    }
}
